package com.df.ez_game.server.gateway.tcp;

import com.df.ez_game.server.NettyClientSettings;
import com.df.ez_game.server.NettyServerSettings;
import com.df.ez_game.server.ServerNode;
import com.df.ez_game.tools.OsHelper;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

public class GatewayTcpNettyServerSettings implements NettyServerSettings {
    @Override
    public EventLoopGroup boosGroup() {
//        int size = Runtime.getRuntime().availableProcessors();
//        if(OsHelper.isWindow()){
//            return new NioEventLoopGroup(size, new ThreadFactory() {
//                @Override
//                public Thread newThread(Runnable r) {
//                    Thread thread = new Thread(r,"gateway-boss-thread");
//                    thread.setDaemon(true);
//                    return thread;
//                }
//            });
//        }
        //TODO 线程的名字自定义
        return OsHelper.getGroupByOs();
    }

    @Override
    public EventLoopGroup worGroup() {
        return OsHelper.getGroupByOs();
    }

    @Override
    public Class<? extends ServerChannel> channel() {
        return NioServerSocketChannel.class;
    }

    @Override
    public void settingServerBootStrap(ServerBootstrap bootstrap) {
        //设置option
        bootstrap.option(ChannelOption.SO_KEEPALIVE,true)
                .childOption(ChannelOption.SO_KEEPALIVE,true)
                .option(ChannelOption.SO_BACKLOG,100)
                .childOption(ChannelOption.TCP_NODELAY,true);
        //设置handler
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {
                //协议编解码
                socketChannel.pipeline().addLast(new LengthFieldBasedFrameDecoder(
                        5*1024*1024,
                        // 长度字段的偏移量， 不偏移
                        0,
                        // 字段的长度,
                        4,
                        // 要添加到长度字段值的补偿值：长度调整值 = 内容字段偏移量 - 长度字段偏移量 - 长度字段的字节数
                        0,
                        // 跳过的初始字节数： 跳过0位; (跳过消息头的 0 位长度)
                        0));
                //心跳相关
//                socketChannel.pipeline().addLast();
                //消息解析
//                socketChannel.pipeline().addLast(new TcpGateWayCodec(serverNode));
////                //TODO 过滤器执行，比如检查cmd是否正确，某些cmd需要认证后才能访问等
//                socketChannel.pipeline().addLast(new GateWayLifeCycleHandler(serverNode));
//                //转发给broker
//                socketChannel.pipeline().addLast(new TcpGateWayForwarding(brokerManager));
            }
        });
    }


    @Override
    public Integer serverPort() {
        return 0;
    }

    @Override
    public void setServerPort(Integer port) {

    }

    @Override
    public void setServerNode(ServerNode serverNode) {

    }
}
